package org.thukari.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import org.thukari.entity.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DealWriteOutService {
    public void writeOut(Map<String, DealCountData> resultMap) {
        String fileName = System.getProperty("user.home") + "/Desktop/outList2.xlsx";
        try (ExcelWriter excelWriter = EasyExcel.write(fileName).build()) {
            WriteSheet allSheet = EasyExcel.writerSheet(0, "数量").head(DealTypeData.class).build();
            WriteSheet dealSheet = EasyExcel.writerSheet(1, "金额").head(DealPriceData.class).build();

            writeAllSheet(resultMap, excelWriter, allSheet);
            writeDealSheet(resultMap, excelWriter, dealSheet);
        }
    }

    private void writeAllSheet(Map<String, DealCountData> resultMap, ExcelWriter excelWriter, WriteSheet allSheet) {
        List<DealTypeData> dealPriceDataList = new ArrayList<>();
        for (Map.Entry<String, DealCountData> dealCountDataEntry : resultMap.entrySet()) {
            DealTypeData dealTypeData = new DealTypeData();
            dealTypeData.setDistrict(dealCountDataEntry.getKey());
            Map<String, DealCountData> districtMap = dealCountDataEntry.getValue().getDataMap();
            int newTotal = 0;
            int changeTotal = 0;
            for (Map.Entry<String, DealCountData> entry : districtMap.entrySet()) {
                switch (entry.getKey()) {
                    case "01新装":
                        newTotal = getNewTotal(entry, dealTypeData, newTotal);
                        break;
                    case "02加装副卡":
                        dealTypeData.setType02(entry.getValue().getCount());
                        changeTotal += entry.getValue().getCount();
                        break;
                    case "03加装二宽":
                        dealTypeData.setType03(entry.getValue().getCount());
                        changeTotal += entry.getValue().getCount();
                        break;
                    case "04加装ITV":
                        dealTypeData.setType04(entry.getValue().getCount());
                        changeTotal += entry.getValue().getCount();
                        break;
                    case "05加装固话":
                        dealTypeData.setType05(entry.getValue().getCount());
                        changeTotal += entry.getValue().getCount();
                        break;
                    case "06加流量包":
                        dealTypeData.setType06(entry.getValue().getCount());
                        changeTotal += entry.getValue().getCount();
                        break;
                    case "07提速包":
                        dealTypeData.setType07(entry.getValue().getCount());
                        changeTotal += entry.getValue().getCount();
                        break;
                    case "08加语音包":
                        dealTypeData.setType08(entry.getValue().getCount());
                        changeTotal += entry.getValue().getCount();
                        break;
                    case "09增值业务":
                        dealTypeData.setType09(entry.getValue().getCount());
                        changeTotal += entry.getValue().getCount();
                        break;
                    case "10提速":
                        dealTypeData.setType10(entry.getValue().getCount());
                        changeTotal += entry.getValue().getCount();
                        break;
                    case "11移机":
                        dealTypeData.setType11(entry.getValue().getCount());
                        changeTotal += entry.getValue().getCount();
                        break;
                    case "12补换卡":
                        dealTypeData.setType12(entry.getValue().getCount());
                        changeTotal += entry.getValue().getCount();
                        break;
                    case "13改信息":
                        dealTypeData.setType13(entry.getValue().getCount());
                        changeTotal += entry.getValue().getCount();
                        break;
                    case "14其它":
                        dealTypeData.setType14(entry.getValue().getCount());
                        changeTotal += entry.getValue().getCount();
                        break;
                    case "15停拆机":
                        dealTypeData.setType15(entry.getValue().getCount());
                        changeTotal += entry.getValue().getCount();
                        break;
                }
            }
            dealTypeData.setNewTotal(newTotal);
            dealTypeData.setChangeTotal(changeTotal);
            dealTypeData.setTotal(dealCountDataEntry.getValue().getCount());
            dealPriceDataList.add(dealTypeData);
        }
        excelWriter.write(dealPriceDataList, allSheet);
    }

    private int getNewTotal(Map.Entry<String, DealCountData> entry, DealTypeData dealTypeData, int newTotal) {
        for (Map.Entry<String, DealCountData> setEntry : entry.getValue().getDataMap().entrySet()) {
            switch (setEntry.getKey()) {
                case "101畅联无忧":
                    dealTypeData.setType101(setEntry.getValue().getCount());
                    newTotal += setEntry.getValue().getCount();
                    break;
                case "102商企ICT":
                    dealTypeData.setType102(setEntry.getValue().getCount());
                    newTotal += setEntry.getValue().getCount();
                    break;
                case "102商企ICT-内网检测":
                    dealTypeData.setType1021(setEntry.getValue().getCount());
                    newTotal += setEntry.getValue().getCount();
                    break;
                case "103融合":
                    dealTypeData.setType103(setEntry.getValue().getCount());
                    newTotal += setEntry.getValue().getCount();
                    break;
                case "104专线-电路":
                    dealTypeData.setType104(setEntry.getValue().getCount());
                    newTotal += setEntry.getValue().getCount();
                    break;
                case "104专线-互专":
                    dealTypeData.setType1041(setEntry.getValue().getCount());
                    newTotal += setEntry.getValue().getCount();
                    break;
                case "104专线-医保":
                    dealTypeData.setType1042(setEntry.getValue().getCount());
                    newTotal += setEntry.getValue().getCount();
                    break;
                case "105FTTR":
                    dealTypeData.setType105(setEntry.getValue().getCount());
                    newTotal += setEntry.getValue().getCount();
                    break;
                case "106酒美":
                    dealTypeData.setType106(setEntry.getValue().getCount());
                    newTotal += setEntry.getValue().getCount();
                    break;
                case "107单宽":
                    dealTypeData.setType107(setEntry.getValue().getCount());
                    newTotal += setEntry.getValue().getCount();
                    break;
                case "108单C":
                    dealTypeData.setType108(setEntry.getValue().getCount());
                    newTotal += setEntry.getValue().getCount();
                    break;
                case "109固话":
                    dealTypeData.setType109(setEntry.getValue().getCount());
                    newTotal += setEntry.getValue().getCount();
                    break;
                case "110中继线":
                    dealTypeData.setType110(setEntry.getValue().getCount());
                    newTotal += setEntry.getValue().getCount();
                    break;
                case "111ITV":
                    dealTypeData.setType111(setEntry.getValue().getCount());
                    newTotal += setEntry.getValue().getCount();
                    break;
                case "112天通卫星":
                    dealTypeData.setType112(setEntry.getValue().getCount());
                    newTotal += setEntry.getValue().getCount();
                    break;
                case "113物联网":
                    dealTypeData.setType113(setEntry.getValue().getCount());
                    newTotal += setEntry.getValue().getCount();
                    break;
                case "114云-AI+云办公":
                    dealTypeData.setType1141(setEntry.getValue().getCount());
                    newTotal += setEntry.getValue().getCount();
                    break;
                case "114云-云电脑":
                    dealTypeData.setType1142(setEntry.getValue().getCount());
                    newTotal += setEntry.getValue().getCount();
                    break;
                case "114云-云眼":
                    dealTypeData.setType1143(setEntry.getValue().getCount());
                    newTotal += setEntry.getValue().getCount();
                    break;
                case "114云-云主机":
                    dealTypeData.setType1144(setEntry.getValue().getCount());
                    newTotal += setEntry.getValue().getCount();
                    break;
                case "115战新-安全大脑":
                    dealTypeData.setType1151(setEntry.getValue().getCount());
                    newTotal += setEntry.getValue().getCount();
                    break;
                case "115战新-绿色照明":
                    dealTypeData.setType1152(setEntry.getValue().getCount());
                    newTotal += setEntry.getValue().getCount();
                    break;
                case "115战新-翼办公":
                    dealTypeData.setType1153(setEntry.getValue().getCount());
                    newTotal += setEntry.getValue().getCount();
                    break;
                case "115战新-云灾备":
                    dealTypeData.setType1154(setEntry.getValue().getCount());
                    newTotal += setEntry.getValue().getCount();
                    break;
                case "120其它":
                    dealTypeData.setType120(setEntry.getValue().getCount());
                    newTotal += setEntry.getValue().getCount();
                    break;
            }
        }
        return newTotal;
    }


    private void writeDealSheet(Map<String, DealCountData> resultMap, ExcelWriter excelWriter, WriteSheet allSheet) {
        List<DealPriceData> dealPriceDataList = new ArrayList<>();
        for (Map.Entry<String, DealCountData> dealCountDataEntry : resultMap.entrySet()) {
            DealPriceData dealPriceData = new DealPriceData();
            dealPriceData.setDistrict(dealCountDataEntry.getKey());
            Map<String, DealCountData> dealCountDataMap = dealCountDataEntry.getValue().getDataMap();
            double newTotal = 0;
            double changeTotal = 0;
            for (Map.Entry<String, DealCountData> entry : dealCountDataMap.entrySet()) {
                switch (entry.getKey()) {
                    case "01新装":
                        newTotal = getNewPrice(entry, dealPriceData, newTotal);
                        break;
                    case "02加装副卡":
                        dealPriceData.setType02(entry.getValue().getMoney());
                        changeTotal += entry.getValue().getMoney();
                        break;
                    case "03加装二宽":
                        dealPriceData.setType03(entry.getValue().getMoney());
                        changeTotal += entry.getValue().getMoney();
                        break;
                    case "04加装ITV":
                        dealPriceData.setType04(entry.getValue().getMoney());
                        changeTotal += entry.getValue().getMoney();
                        break;
                    case "05加装固话":
                        dealPriceData.setType05(entry.getValue().getMoney());
                        changeTotal += entry.getValue().getMoney();
                        break;
                    case "06加流量包":
                        dealPriceData.setType06(entry.getValue().getMoney());
                        changeTotal += entry.getValue().getMoney();
                        break;
                    case "07提速包":
                        dealPriceData.setType07(entry.getValue().getMoney());
                        changeTotal += entry.getValue().getMoney();
                        break;
                    case "08加语音包":
                        dealPriceData.setType08(entry.getValue().getMoney());
                        changeTotal += entry.getValue().getMoney();
                        break;
                    case "09增值业务":
                        dealPriceData.setType09(entry.getValue().getMoney());
                        changeTotal += entry.getValue().getMoney();
                        break;
                    case "10提速":
                        dealPriceData.setType10(entry.getValue().getMoney());
                        changeTotal += entry.getValue().getMoney();
                        break;
                    case "11移机":
                        dealPriceData.setType11(entry.getValue().getMoney());
                        changeTotal += entry.getValue().getMoney();
                        break;
                    case "12补换卡":
                        dealPriceData.setType12(entry.getValue().getMoney());
                        changeTotal += entry.getValue().getMoney();
                        break;
                    case "13改信息":
                        dealPriceData.setType13(entry.getValue().getMoney());
                        changeTotal += entry.getValue().getMoney();
                        break;
                    case "14其它":
                        dealPriceData.setType14(entry.getValue().getMoney());
                        changeTotal += entry.getValue().getMoney();
                        break;
                    case "15停拆机":
                        dealPriceData.setType15(entry.getValue().getCount());
                        changeTotal += entry.getValue().getCount();
                        break;
                }
            }
            dealPriceData.setNewTotal(newTotal);
            dealPriceData.setChangeTotal(changeTotal);
            dealPriceData.setTotal(dealCountDataEntry.getValue().getMoney());
            dealPriceDataList.add(dealPriceData);
        }
        excelWriter.write(dealPriceDataList, allSheet);
    }

    private double getNewPrice(Map.Entry<String, DealCountData> entry, DealPriceData dealPriceData, double newTotal) {
        for (Map.Entry<String, DealCountData> setEntry : entry.getValue().getDataMap().entrySet()) {
            switch (setEntry.getKey()) {
                case "101畅联无忧":
                    dealPriceData.setType101(setEntry.getValue().getMoney());
                    newTotal += setEntry.getValue().getMoney();
                    break;
                case "102商企ICT":
                    dealPriceData.setType102(setEntry.getValue().getMoney());
                    newTotal += setEntry.getValue().getMoney();
                    break;
                case "102商企ICT-内网检测":
                    dealPriceData.setType1021(setEntry.getValue().getMoney());
                    newTotal += setEntry.getValue().getMoney();
                    break;
                case "103融合":
                    dealPriceData.setType103(setEntry.getValue().getMoney());
                    newTotal += setEntry.getValue().getMoney();
                    break;
                case "104专线-电路":
                    dealPriceData.setType104(setEntry.getValue().getMoney());
                    newTotal += setEntry.getValue().getMoney();
                    break;
                case "104专线-互专":
                    dealPriceData.setType1041(setEntry.getValue().getMoney());
                    newTotal += setEntry.getValue().getMoney();
                    break;
                case "104专线-医保":
                    dealPriceData.setType1042(setEntry.getValue().getMoney());
                    newTotal += setEntry.getValue().getMoney();
                    break;
                case "105FTTR":
                    dealPriceData.setType105(setEntry.getValue().getMoney());
                    newTotal += setEntry.getValue().getMoney();
                    break;
                case "106酒美":
                    dealPriceData.setType106(setEntry.getValue().getMoney());
                    newTotal += setEntry.getValue().getMoney();
                    break;
                case "107单宽":
                    dealPriceData.setType107(setEntry.getValue().getMoney());
                    newTotal += setEntry.getValue().getMoney();
                    break;
                case "108单C":
                    dealPriceData.setType108(setEntry.getValue().getMoney());
                    newTotal += setEntry.getValue().getMoney();
                    break;
                case "109固话":
                    dealPriceData.setType109(setEntry.getValue().getMoney());
                    newTotal += setEntry.getValue().getMoney();
                    break;
                case "110中继线":
                    dealPriceData.setType110(setEntry.getValue().getMoney());
                    newTotal += setEntry.getValue().getMoney();
                    break;
                case "111ITV":
                    dealPriceData.setType111(setEntry.getValue().getMoney());
                    newTotal += setEntry.getValue().getMoney();
                    break;
                case "112天通卫星":
                    dealPriceData.setType112(setEntry.getValue().getMoney());
                    newTotal += setEntry.getValue().getMoney();
                    break;
                case "113物联网":
                    dealPriceData.setType113(setEntry.getValue().getMoney());
                    newTotal += setEntry.getValue().getMoney();
                    break;
                case "114云-AI+云办公":
                    dealPriceData.setType1141(setEntry.getValue().getMoney());
                    newTotal += setEntry.getValue().getMoney();
                    break;
                case "114云-云电脑":
                    dealPriceData.setType1142(setEntry.getValue().getMoney());
                    newTotal += setEntry.getValue().getMoney();
                    break;
                case "114云-云眼":
                    dealPriceData.setType1143(setEntry.getValue().getMoney());
                    newTotal += setEntry.getValue().getMoney();
                    break;
                case "114云-云主机":
                    dealPriceData.setType1144(setEntry.getValue().getMoney());
                    newTotal += setEntry.getValue().getMoney();
                    break;
                case "115战新-安全大脑":
                    dealPriceData.setType1151(setEntry.getValue().getMoney());
                    newTotal += setEntry.getValue().getMoney();
                    break;
                case "115战新-绿色照明":
                    dealPriceData.setType1152(setEntry.getValue().getMoney());
                    newTotal += setEntry.getValue().getMoney();
                    break;
                case "115战新-翼办公":
                    dealPriceData.setType1153(setEntry.getValue().getMoney());
                    newTotal += setEntry.getValue().getMoney();
                    break;
                case "115战新-云灾备":
                    dealPriceData.setType1154(setEntry.getValue().getMoney());
                    newTotal += setEntry.getValue().getMoney();
                    break;
                case "120其它":
                    dealPriceData.setType120(setEntry.getValue().getMoney());
                    newTotal += setEntry.getValue().getMoney();
                    break;
            }
        }
        return newTotal;
    }
}
